<?php
class Admin_NhanVienController extends Zend_Controller_Action{

    public function init()
    {
         $this->_helper->layout->setLayout('admin');
    }

    public function indexAction()
    {
    	$nhanvienMapper = Library_Model_NhanVienMapper::getInstance();
    	$nhanvien = $nhanvienMapper->nhanvienList();
    	$this->view->nhanvien = $nhanvien;
    }

    public function themAction()
    {
    	$this->view->title = 'Thêm nhân viên';
    	$formThem = new Admin_Form_NhanVienForm();
    	$formThem->setMethod('POST');
    	$formThem->setAction('/zfMHB/public/admin/nhanvien/them');
    	
    	$formThem->addElement('submit', 'submit', array('label' => 'Thêm'));
    	$formThem->getElement('tennhanvien')->addValidator('Db_NoRecordExists',true, array(
    			'table' => 'tbl_nhanvien',
    			'field' => 'TenNhanVien'
    	));
    	$this->view->form = $formThem;
    	
    	if($this->getRequest()->isPost()){
    		if($formThem->isValid($_POST)){
    			$data = $formThem->getValues();
    			
    			$maphongban 			= $data['maphongban'];
    			$machucvu					= $data['machucvu'];
    			$tennhanvien 				= $data['tennhanvien'];
    			$namsinh						= $data['namsinh'];
    			$diachi 						= $data['diachi'];
    			$dienthoai					= $data['dienthoai'];
    			
    			$nhanvienMapper = Library_Model_NhanVienMapper::getInstance();
    			$nhanvienObj 	= new Library_Model_NhanVien();
    			    			
    			$nhanvienObj->setMaphongban($maphongban);
    			$nhanvienObj->setMachucvu($machucvu);
    			$nhanvienObj->setTennhanvien($tennhanvien);
    			$nhanvienObj->setNamsinh($namsinh);
    			$nhanvienObj->setDiachi($diachi);
    			$nhanvienObj->setDienthoai($dienthoai);
    	
    			$nhanvien = $nhanvienMapper->save($nhanvienObj);
    			$this->_redirect('/admin/nhanvien/index');
    		}
    	} 
    }

    public function capnhatAction()
    {
        $this->view->title = 'Cập nhật nhân viên';
        $formCapnhat = new Admin_Form_NhanVienForm();        
        $formCapnhat->setMethod('post');
        $formCapnhat->setAction('/zfMHB/public/admin/nhanvien/capnhat/');
        
        $formCapnhat->addElement('submit', 'submit', array('label' => 'Lưu'));
        $formCapnhat->addElement('hidden', 'id');
        $manhanvien = $this->getRequest()->getParam('id');
        
        $formCapnhat->getElement('tennhanvien')->addValidator('Db_NoRecordExists',true, array(
        		'table' => 'tbl_nhanvien',
        		'field' => 'TenNhanVien',
        		'exclude' => array(
        				'field' => 'MaNhanVien',
        				'value' => $manhanvien)
        ));
        
        $formCapnhat->getElement('tennhanvien')->getValidator('Db_NoRecordExists')->setMessage('Nhân viên đã tồn tại!');
        $nhanvienMapper = Library_Model_NhanVienMapper::getInstance();
        $this->view->form = $formCapnhat;
        
        if($this->getRequest()->isPost()){
        	if($formCapnhat->isValid($_POST)){
        		$data = $formCapnhat->getValues();
        		
        		$id 					= $data['id'];
        		$maphongban 						= $data['maphongban'];
    			$machucvu								= $data['machucvu'];
    			$tennhanvien 						= $data['tennhanvien'];
    			$namsinh								= $data['namsinh'];
    			$diachi 						= $data['diachi'];
    			$dienthoai					= $data['dienthoai'];
    			
    			$nhanvienObj 	= new Library_Model_NhanVien();

    			$nhanvienObj->setId($id);
    			$nhanvienObj->setMaphongban($maphongban);
    			$nhanvienObj->setMachucvu($machucvu);
    			$nhanvienObj->setTennhanvien($tennhanvien);
    			$nhanvienObj->setNamsinh($namsinh);
    			$nhanvienObj->setDiachi($diachi);
    			$nhanvienObj->setDienthoai($dienthoai);
    	
    			$nhanvien = $nhanvienMapper->save($nhanvienObj);
    			$this->_redirect('/admin/nhanvien/index');
        	}
        } else {
        		$nhanvien = $nhanvienMapper->searchNhanVienById($manhanvien);
        		
        		$formCapnhat->getElement('id')->setValue($nhanvien->getId());
        		$formCapnhat->getElement('maphongban')->setValue($nhanvien->getMaphongban());
        		$formCapnhat->getElement('machucvu')->setValue($nhanvien->getMachucvu());
        		$formCapnhat->getElement('tennhanvien')->setValue($nhanvien->getTennhanvien());
        		$formCapnhat->getElement('namsinh')->setValue($nhanvien->getNamsinh());
        		$formCapnhat->getElement('diachi')->setValue($nhanvien->getDiachi());
        		$formCapnhat->getElement('dienthoai')->setValue($nhanvien->getDienthoai());
        	}
        
    }

    public function xoaAction()
    {
        $this->_helper->viewRenderer->setNoRender(true);
        $manhanvien = $this->getRequest()->getParam('id');
        $nhanvienMapper = Library_Model_NhanVienMapper::getInstance();
        $nhanvienMapper->delete($manhanvien);
        $this->_redirect('/admin/nhanvien/index');
    }
    
    
/*     public function validateformAction()
    {
        $user_id = $this->getRequest()->getParam('id');
        $this->_helper->viewRenderer()->setNoRender();
        $this->_helper->getHelper('layout')->disableLayout();
        $form = new Admin_Form_NhanVienForm();
        $form->isValid($this->_getAllParams());
        $json = $form->getMessages();
        header('Content-type: application/json');
        echo Zend_Json::encode($json);
    }
 */

}









